package com.matt.controller.web.courseV2;

import com.dotflat.xj.domain.Data;
import com.dotflat.xj.service.DataService;
import com.dotflat.util.AssertUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.courseV2.VendorCoursePackage;
import com.matt.domain.courseV2.po.VendorCoursePackagePO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.courseV2.VendorCoursePackageService;
import com.matt.service.courseV2.VendorCourseService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/api/course_Package")
@Slf4j
@Api(tags = "教学 - 课程包管理")
public class CoursePackageController {

    @Autowired
    private VendorCoursePackageService vendorCoursePackageService;

    @Autowired
    private VendorCourseService courseService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private DataService dataService;

    //课程包分类
    private static  final String  KCB_SORT = "kcb_sort";

    @ApiOperation(value = "园区课程列表")
    @PostMapping("/courseList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string")
    })
    public StatusResponse  courseList(@RequestHeader(TICKET) String ticket) {
        return StatusResponse.success(vendorCoursePackageService.courseList(ticket));
    }

    @ApiOperation(value = "园区课程包列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "0托班1早教", required = true, dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 000000,message = "successful",response = VendorCoursePackagePO.class),
    })
    public StatusResponse  list(@RequestHeader(TICKET) String ticket,int type, PagerQuery pagerQuery) {
        StaffDTO staffDTO =  cache.getStaff(ticket);
        if (AssertUtil.isEmpty(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        List<VendorCoursePackage> list = vendorCoursePackageService.list(staffDTO.getAreaId(),type,pagerQuery);

        Integer total = vendorCoursePackageService.count(staffDTO.getAreaId(),type);

        List<Data>  sortList = dataService.findAllByType(KCB_SORT);
        Map map = new HashMap();
        map.put("total",total);
        map.put("list",list);
        map.put("sortList",sortList);
        return StatusResponse.success(map);
    }
}
